<?php 
set_time_limit(0);
ini_set("memory_limit", "12M");
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
unset($_SESSION['s_cameras']);

include 'checkdate.php';
//$s_date = $_REQUEST['s_date'];
 
if (isset($_REQUEST['m_fromdate']) and isset($_REQUEST['m_todate']) and isset($_REQUEST['s_monitor'])) {
	include '../connection/connection.php';
	$m_fromdate = $_REQUEST['m_fromdate']; 
	$m_todate = $_REQUEST['m_todate'];
	$s_monitor = $_REQUEST['s_monitor'];
	$winH = 520;
	if(isset($_REQUEST['winH'])) $winH = $_REQUEST['winH'];
	$monitorid = base64_encode($s_monitor);//monitorid
	if($s_monitor!='0'){
	    $sql="select co.id,co.name,ca.isReg,count(*) as count1 from configcamera co, carnum ca where ca.car_datetime >='$m_fromdate' and ca.car_datetime <='$m_todate' and co.id=ca.id and ca.id in (select camera_ID from favourite where monitor_ID='$s_monitor') GROUP BY co.id,ca.isReg";
	}
	else {
	    $sql="select co.id,co.name,ca.isReg,count(*) as count1 from configcamera co, carnum ca where ca.car_datetime >='$m_fromdate' and ca.car_datetime <='$m_todate' and co.id=ca.id  GROUP BY co.id,ca.isReg";
		
	}
	$listSearch = mysql_query($sql,$connection);
	$index = 0;
	$count = 0;
	$isReg = 0;
	$isNoReg = 0;
	$mylist = array();
	?>
	<div id="divscroll" style="overflow:scroll;overflow-x: hidden; width:100%; height:<?=$winH?>px; background-color: #ffffff">
	               <table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" style="margin-top:0" id="tablebody">
						 <tbody>
						 
	<?php 
	$viewcamera = "보기";
	while ($row = mysql_fetch_array($listSearch)){
				$count+=1;
				if($count==1){
					$obj1 = $row;
				}else if ($count==2) {
					$obj2 = $row;
				}
				if ($count==2){
					if($obj1[id] == $obj2[id]){
						if ($obj1[isReg] == 1) {
							$isReg = $obj1[count1];
							$isNoReg = $obj2[count1];
						}
						else{
							$isReg = $obj2[count1];
							$isNoReg = $obj1[count1];
						}
						$count = 0;
					$index+=1;
					$percents = ($isReg/($isReg+$isNoReg))*100;
					$indexD = stripos($percents, '.');
					if ($indexD != FALSE) {
						$percents = substr($percents,0,$indexD);
					}
					$counts = $isReg+$isNoReg;
					 $mylist[$index] = array($row[id],$row[name],$counts,$isReg,$isNoReg,$percents,$m_fromdate,$m_todate,$monitorid);
					 
					 ?>
					 <tr>
					 	<td width="6%"><?=$index?></td>
					 	<td width="6%"><?=$row[id]?></td>
					 	<td width="21%"><?=$row[name]?></td>
					 	<td width="11%"><?=$counts?></td>
					 	<td width="11%"><?=$isReg?></td>
					 	<td width="11%"><?=$isNoReg?></td>
					 	<td width="11%" <?php if($percents == 100){?>style="color:#00FFB6"<?php }elseif($percents < 50 ){?>style="color:#FF3A3A"<?php }?> ><?=$percents?>%</td>
					 	<td width="11%"  class="last" align="center"><a style="color: #0000FF;cursor: pointer;" onclick="OpenView(this,'<?=$monitorid?>','<?=base64_encode($row[id])?>','<?=base64_encode($m_todate)?>','<?=base64_encode($m_fromdate)?>')"><?=$viewcamera?></a></td>
					 </tr>  
					 <?php 
					 
				} else{
					if ($obj1[isReg] == 1) {
						$isReg = $obj1[count1];
						$isNoReg = 0;
					}
					else{
						$isNoReg = $obj1[count1];
						$isReg = 0;
					}
					$count = 1;
					$index+=1;
					$percents = ($isReg/($isReg+$isNoReg))*100;
					$indexD = stripos($percents, '.');
					if ($indexD != FALSE) {
						$percents = substr($percents,0,$indexD);
					}
					$counts = $isReg+$isNoReg;
					 $mylist[$index] = array($obj1[id],$obj1[name],$counts,$isReg,$isNoReg,$percents,$m_fromdate,$m_todate,$monitorid);
					
					 ?>
					 <tr>
					 	<td width="6%"><?=$index?></td>
					 	<td width="6%"><?=$obj1[id]?></td>
					 	<td width="21%"><?=$obj1[name]?></td>
					 	<td width="11%"><?=$counts?></td>
					 	<td width="11%"><?=$isReg?></td>
					 	<td width="11%"><?=$isNoReg?></td>
					 	<td width="11%" <?php if($percents == 100){?>style="color:#00FFB6"<?php }elseif($percents < 50 ){?>style="color:#FF3A3A"<?php }?> ><?=$percents?>%</td>
					 	<td width="11%"  class="last" align="center"><a style="color: #0000FF;cursor: pointer;" onclick="OpenView(this,'<?=$monitorid?>','<?=base64_encode($obj1[id])?>','<?=base64_encode($m_todate)?>','<?=base64_encode($m_fromdate)?>')">보기</a></td>
					 </tr>  
					 <?php 
					 
					 $obj1 = $obj2;
					
				}
				
	}}
	if ($index>0 and $index<$winH/28) {
	 	foreach(range(0,$winH/28-$index) as $i){
	 		?>
	 		<tr>
		 	<td width="6%">&nbsp;</td>
		 	<td width="6%">&nbsp;</td>
		 	<td width="21%">&nbsp;</td>
		 	<td width="11%">&nbsp;</td>
		 	<td width="11%">&nbsp;</td>
		 	<td width="11%">&nbsp;</td>
		 	<td width="11%">&nbsp;</td>
		 	<td width="11%"  class="last">&nbsp;</td>
		 	</tr>
	 	<?php }
	}
	?>
	
	</tbody>       
		               </table>
	</div>
	<?php 
	$_SESSION['s_cameras'] =  $mylist;
	
	mysql_close($connection);
	  
}
?>

